<div class="layui-card">
    <div class="layui-card-header">
        <h2 class="header-title">文件中心</h2>
        <span class="layui-breadcrumb pull-right">
          <a href="#!home_console">首页</a>
          <a><cite>文件中心</cite></a>
        </span>
    </div>
    <div class="layui-card-body">
        <div class="layui-form toolbar">
            搜索：<input id="files-edit-search" class="layui-input search-input" type="text" placeholder="输入关键字"/>&emsp;
            <button id="files-btn-search" class="layui-btn icon-btn"><i class="layui-icon">&#xe615;</i>搜索</button>
        </div>

        <!-- 数据表格 -->
        <table class="layui-table" id="files-table" lay-filter="files-table"></table>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="files-table-bar">
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>



<script>

    layui.use(['form', 'table', 'util', 'config', 'admin'],function () {
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var layer = layui.layer;
        var util = layui.util;
        var admin = layui.admin;


        // 渲染表格
        table.render({
            elem: '#files-table',
            // url: config.base_server + 'user_query.json',
            url: config.base_server + 'api-file/files',
            method: 'GET',
            where: {
                access_token: config.getToken().access_token
            },
            page: true,
            cols: [[
                {type: 'numbers'},
                {field: 'id', sort: true, title: '编号'},
                {field: 'name', sort: true, title: '流媒体名称'},
                {field: 'size', sort: true, title: '文件大小(B)'},
                {field: 'contentType', sort: true, title: '媒体类型'},
                {field: 'source', sort: true, title: 'OSS运营商'},
                {
                    sort: true, templet: function (d) {
                        return util.toDateString(d.createTime);
                    }, title: '创建时间'
                },
                {
                    field: 'url', width: 250, align: 'center', templet: function (d) {
                        if (d.isImg){
                            return '<img src="'+d.url+'" class="layui-badge layui-bg-gray"></img>';
                        }else {
                            return '<a href="'+d.url+'" class="layui-badge layui-bg-gray">'+d.name+'</a>';
                        }
                    }, title: '内容'
                },
                {align: 'center', width:80, toolbar: '#files-table-bar', title: '操作'}
            ]]
        });

        // 工具条点击事件
        table.on('tool(files-table)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;

            if (layEvent === 'del') { // 删除
                doDelete(obj);
            }
        });


        // 删除
        var doDelete = function (obj) {
            layer.confirm('确定要删除吗？', function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('api-file/files/' + obj.data.id, {}, function (data) {
                    layer.closeAll('loading');
                    if (data.resp_code == 0) {
                        layer.msg(data.resp_msg, {icon: 1, time: 500});
                        obj.del();
                    } else {
                        layer.msg(data.resp_msg, {icon: 2, time: 500});
                    }
                }, 'DELETE');
            });
        };


        // 搜索按钮点击事件
        $('#files-btn-search').click(function () {
            var key = $('#files-edit-search').val();
            table.reload('files-table', {where: {searchKey: key}});
        });
    });


</script>
